﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace POSPlus.PrintManager
{
	public class RegularPrintInfo : DatabaseView,/* IComparable<RegularPrintInfo>,*/ IRegular
	{
		private string _certCode;

		public int LocAisle { get; private set; }
		public int LocBlock { get; private set; }
		public int LocSeq { get; private set; }
		public String Upc { get; private set; }
		public string CertCode
		{
			get { return string.IsNullOrEmpty(_certCode) ? "" : _certCode; }
			private set
			{
				_certCode = value;
			}
		}		public String Description { get; private set; }
		public decimal NormalPrice { get; private set; }
		public short PriceMethod { get; private set; }
		public decimal GroupPrice { get; private set; }
		public short Quantity { get; private set; }
		public int Vendor { get; private set; }
		public short Pack { get; private set; }
		public String Size { get; private set; }
		public double UnitNP { get; private set; }
		public String Uom { get; private set; }
		public double AvgDailyMovement { get; private set; }
		public int Hosted { get; private set; }
		public string BatchNo { get { return ""; } }
		public RegularPrintInfo(string connectionString)
			: base(connectionString)
		{	}

		public RegularPrintInfo(String connectionString, string upc, int locAisle, int locBlock, int locSeq) : base(connectionString)
		{
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_RegularPrintInfo WHERE " +
					"Upc = @Upc AND ISNULL(LocAisle, 0) = ISNULL(@LocAisle,0) AND ISNULL(LocBlock, 0) = ISNULL(@LocBlock, 0) " +
					"AND ISNULL(LocSeq, 0) = ISNULL(@LocSeq, 0)", connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					command.Parameters.AddWithValue("@LocAisle", locAisle);
					command.Parameters.AddWithValue("@LocBlock", locBlock);
					command.Parameters.AddWithValue("@LocSeq", locSeq);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							LocAisle = FromDB<int>(reader["LocAisle"]);
							LocBlock = FromDB<int>(reader["LocBlock"]);
							LocSeq = FromDB<int>(reader["LocSeq"]);
							Upc = FromDB<String>(reader["Upc"]);
							CertCode = FromDB<string>(reader["Cert_Code"]);
							Description = FromDB<String>(reader["Description"]);
							NormalPrice = FromDB<decimal>(reader["Normal_Price"]);
							PriceMethod = FromDB<short>(reader["PriceMethod"]);
							GroupPrice = FromDB<decimal>(reader["GroupPrice"]);
							Quantity = FromDB<short>(reader["Quantity"]);
							Vendor = FromDB<int>(reader["Vendor"]);
							Pack = FromDB<short>(reader["Pack"]);
							Size = FromDB<String>(reader["Size"]);
							UnitNP = FromDB<double>(reader["Unit_NP"]);
							Uom = FromDB<String>(reader["Uom"]);
							AvgDailyMovement = FromDB<double>(reader["AvgDailyMovement"]);
							Hosted = FromDB<int>(reader["Hosted"]);
						}
					}
				}
				connection.Close();
			}
		}
		/*
		internal static List<RegularPrintInfo> Get(string connectionString, string jobName)
		{
			List<RegularPrintInfo> info = new List<RegularPrintInfo>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_RegularPrintInfo WHERE JobName = @JobName", connection))
				{
					command.Parameters.AddWithValue("@JobName", jobName);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							RegularPrintInfo oneInfo = new RegularPrintInfo(connectionString);
							oneInfo.LocAisle = FromDB<int>(reader["LocAisle"]);
							oneInfo.LocBlock = FromDB<int>(reader["LocBlock"]);
							oneInfo.LocSeq = FromDB<int>(reader["LocSeq"]);
							oneInfo.Upc = FromDB<string>(reader["Upc"]);
							oneInfo.CertCode = FromDB<string>(reader["Cert_Code"]);
							oneInfo.Description = FromDB<string>(reader["Description"]);
							oneInfo.NormalPrice = FromDB<decimal>(reader["Normal_Price"]);
							oneInfo.PriceMethod = FromDB<short>(reader["PriceMethod"]);
							oneInfo.GroupPrice = FromDB<decimal>(reader["GroupPrice"]);
							oneInfo.Quantity = FromDB<short>(reader["Quantity"]);
							oneInfo.Vendor = FromDB<int>(reader["Vendor"]);
							oneInfo.Pack = FromDB<short>(reader["Pack"]);
							oneInfo.Size = FromDB<string>(reader["Size"]);
							oneInfo.UnitNP = FromDB<double>(reader["Unit_NP"]);
							oneInfo.Uom = FromDB<string>(reader["Uom"]);
							oneInfo.AvgDailyMovement = FromDB<double>(reader["AvgDailyMovement"]);
							oneInfo.Hosted = FromDB<int>(reader["Hosted"]);
							info.Add(oneInfo);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return info;
		}
		*/
		public int CompareTo(IRegular other)
		{

			if (LocAisle != 0 && other.LocAisle != 0)
			{
				if (LocAisle < other.LocAisle)
					return -1;
				else if (LocAisle > other.LocAisle)
					return 1;

				if (LocBlock < other.LocBlock)
					return -1;
				else if (LocBlock > other.LocBlock)
					return 1;

				if (LocSeq < other.LocSeq)
					return -1;
				else if (LocSeq > other.LocSeq)
					return 1;
			}

			if (LocAisle == 0 && other.LocAisle != 0)
				return 1;

			if (LocAisle != 0 && other.LocAisle == 0)
				return -1;

			return Upc.CompareTo(other.Upc);
		}

		public List<IRegular> Get(string connectionString, string jobName)
		{
			List<IRegular> info = new List<IRegular>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_RegularPrintInfo WHERE JobName = @JobName", connection))
				{
					command.Parameters.AddWithValue("@JobName", jobName);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							RegularPrintInfo oneInfo = new RegularPrintInfo(connectionString);
							oneInfo.LocAisle = FromDB<int>(reader["LocAisle"]);
							oneInfo.LocBlock = FromDB<int>(reader["LocBlock"]);
							oneInfo.LocSeq = FromDB<int>(reader["LocSeq"]);
							oneInfo.Upc = FromDB<string>(reader["Upc"]);
							oneInfo.CertCode = FromDB<string>(reader["Cert_Code"]);
							oneInfo.Description = FromDB<string>(reader["Description"]);
							oneInfo.NormalPrice = FromDB<decimal>(reader["Normal_Price"]);
							oneInfo.PriceMethod = FromDB<short>(reader["PriceMethod"]);
							oneInfo.GroupPrice = FromDB<decimal>(reader["GroupPrice"]);
							oneInfo.Quantity = FromDB<short>(reader["Quantity"]);
							oneInfo.Vendor = FromDB<int>(reader["Vendor"]);
							oneInfo.Pack = FromDB<short>(reader["Pack"]);
							oneInfo.Size = FromDB<string>(reader["Size"]);
							oneInfo.UnitNP = FromDB<double>(reader["Unit_NP"]);
							oneInfo.Uom = FromDB<string>(reader["Uom"]);
							oneInfo.AvgDailyMovement = FromDB<double>(reader["AvgDailyMovement"]);
							oneInfo.Hosted = FromDB<int>(reader["Hosted"]);
							info.Add(oneInfo);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return info;
		}
	}
}
